/**
 * 
 */
package com.cuc.platform.core.service.logic;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.cuc.platform.core.componet.security.utils.CommonIdentityTools;
import com.cuc.platform.core.dao.baseInfo.AttrDao;
import com.cuc.platform.core.dao.baseInfo.AttrTypeDao;
import com.cuc.platform.core.domain.baseInfo.Attr;
import com.cuc.platform.core.domain.baseInfo.AttrType;
import com.cuc.platform.core.service.AttrManagerService;
import com.cuc.platform.core.service.ServiceException;

/**
 * @author peepul
 *
 * 公共属性管理业务实现类
 */
@Service("AttrManagerService")
public class AttrManagerServiceImpl implements AttrManagerService {

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.service.AttrManagerService#getAttrType(java.lang.Long)
	 */
	@Override
	public AttrType getAttrType(Long id) {
		if(null == id) return null;
		
		return attrTypeDao.getAttrType(id);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.service.AttrManagerService#getAllAttrTypes()
	 */
	@Override
	public List<AttrType> getAllAttrTypes() {
		return attrTypeDao.getAllAttrTypes();
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.service.AttrManagerService#insertAttrType(com.cuc.platform.core.domain.AttrType)
	 */
	@Override
	public void insertAttrType(AttrType attrType) throws ServiceException {
		if(null == attrType) return;
		
		//赋缺省值
		if(null == attrType.getStatus()){
			attrType.setStatus(CommonIdentityTools.IS_ACTIVE);
		}
		
		attrTypeDao.insertAttrType(attrType);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.service.AttrManagerService#updateAttrType(com.cuc.platform.core.domain.AttrType)
	 */
	@Override
	public void updateAttrType(AttrType attrType) throws ServiceException {
		if(null == attrType) return;
		
		//赋缺省值
		if(null == attrType.getStatus()){
			attrType.setStatus(CommonIdentityTools.IS_ACTIVE);
		}
		
		attrTypeDao.updateAttrType(attrType);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.service.AttrManagerService#delAttrType(java.lang.Long)
	 */
	@Override
	public void delAttrType(Long id) throws ServiceException {
		if(null == id) return;
		
		try{
			int rv = attrTypeDao.delAttrType(id);
			
			//如果返回值为0,则没找到对应id的数据，抛出业务异常给web层
			if(rv == 0){
				logger.warn("没找到id为：" + id + "的数据，无效的删除操作。");
				throw new ServiceException("系统中不存在要删除的数据！");
			}
		}catch(DataAccessException ex){
			logger.error("该用户有关联的数据，无效的删除操作。");
			throw new ServiceException("请先删除关联数据，才能删除本条数据。");
		}
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.service.AttrManagerService#getAttrsByType(java.lang.Long)
	 */
	@Override
	public List<Attr> getAttrsByType(Long attrTypeId) {
		if(null == attrTypeId) return null;
		
		return attrDao.getAttrsByType(attrTypeId);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.service.AttrManagerService#delTypeWithAllAttrs(java.lang.Long)
	 */
	@Override
	public void delTypeWithAllAttrs(Long attrTypeId) throws ServiceException {
		if(null == attrTypeId) return;
		
		try{
			//先删除attr对应的数据
			attrDao.delAttrsByTypeId(attrTypeId);
			//再删除attrType中对应的数据
			attrTypeDao.delAttrType(attrTypeId);
		}catch(DataAccessException ex){
			logger.error("无效的删除操作：数据可能被引用，无法删除！");
			throw new ServiceException("删除数据失败，请见详细出错信息。");
		}
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.service.AttrManagerService#getAttr(java.lang.Long)
	 */
	@Override
	public Attr getAttr(Long id) {
		if(null == id) return null;

		return attrDao.getAttr(id);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.service.AttrManagerService#insertAttr(java.lang.Long, com.cuc.platform.core.domain.Attr)
	 */
	@Override
	public void insertAttr(Long attrTypeId, Attr attr) throws ServiceException {
		if(null == attr) return;

		//赋关联id
		if(null == attr.getAttrTypeId()) {
			if(null != attrTypeId){
				attr.setAttrTypeId(attrTypeId);
			} else {
				return;
			}
		} 
		
		//赋缺省值
		if(null == attr.getStatus()){
			attr.setStatus(CommonIdentityTools.IS_ACTIVE);
		}
		
		attrDao.insertAttr(attr);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.service.AttrManagerService#updateAttr(com.cuc.platform.core.domain.Attr)
	 */
	@Override
	public void updateAttr(Attr attr) throws ServiceException {
		//校验
		if(null == attr) {
			return;
		}
		
		//赋缺省值
		if(null == attr.getStatus()){
			attr.setStatus(CommonIdentityTools.IS_ACTIVE);
		}
		
		attrDao.updateAttr(attr);
	}

	/* (non-Javadoc)
	 * @see com.cuc.platform.core.service.AttrManagerService#delAttr(java.lang.Long)
	 */
	@Override
	public void delAttr(Long id) throws ServiceException {
		if(null == id) return;

		try{
			int rv = attrDao.delAttr(id);
			
			//如果返回值为0,则没找到对应id的数据，抛出业务异常给web层
			if(rv == 0){
				logger.warn("没找到id为：" + id + "的数据，无效的删除操作。");
				throw new ServiceException("系统中不存在要删除的数据！");
			}
		}catch(DataAccessException ex){
			logger.error("有外键关联的数据，无效的删除操作。");
			throw new ServiceException("请先删除关联数据，才能删除本条数据。");
		}
	}
	
	/**
	 * 根据条件查询对象列表
	 * 
	 * @param  AttrType
	 */
	public  List<AttrType> queryAttrTypes(AttrType attrType) throws ServiceException{
		return attrTypeDao.queryAttrTypes(attrType);
	}

	//日志
	private static Logger logger = Logger.getLogger(AttrManagerServiceImpl.class);
	
	/**
	 * Ioc Beans
	 */
	@Resource
	private AttrTypeDao attrTypeDao;
	private AttrDao attrDao;
	
	/**
	 * @param attrTypeDao the attrTypeDao to set
	 */
	public void setAttrTypeDao(AttrTypeDao attrTypeDao) {
		this.attrTypeDao = attrTypeDao;
	}

	/**
	 * @param attrDao the attrDao to set
	 */
	public void setAttrDao(AttrDao attrDao) {
		this.attrDao = attrDao;
	}
	
}
